//
//  SegmentedViewControl.swift
//  SwiftLearnAll
//
//  Created by iosdev1 on 2017/7/13.
//  Copyright © 2017年 ygsoft. All rights reserved.
//

import UIKit

class SegmentedViewControl: ZyBaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let segment = UISegmentedControl.init(items: ["0", "1", "2", "3", "4", "5", "6", "7"])
        self.view.addSubview(segment)

        segment.frame = CGRect(x:10, y:80, width:WIDTH_SCREEN - 20, height:70);
        // 设置默认选择项索引
        segment.selectedSegmentIndex = 2;

//        无边框，无背景
        segment.tintColor = UIColor.clear

//        黄色边框黄色背景
//        segment.tintColor = #colorLiteral(red: 0.9816428922, green: 1, blue: 0.2251915377, alpha: 1)

        // 设置在点击后是否恢复原样
        segment.isMomentary = false

        //设置指定索引的题目
        segment.setTitle("指定", forSegmentAt: 1)

        segment.setImage(UIImage.init(named: "girl"), forSegmentAt: 2)

        // 在指定索引插入一个选项并设置图片
        segment.insertSegment(with: UIImage.init(named: "dm1"), at: 3, animated: true);

        // 在指定索引插入一个选项并设置题目
        segment.insertSegment(withTitle: "insert", at: 4, animated: true)

        // 移除指定索引的选项
        segment.removeSegment(at: 5, animated: true)

        // 设置指定索引选项的宽度
        segment.setWidth(70, forSegmentAt: 6);

        // 设置选项中图片等的左上角的位置
        segment.setContentOffset(CGSize(width:10.0, height:10.0), forSegmentAt: 7)

        //获取指定索引选项的图片imageForSegmentAtIndex：
        let image = segment.imageForSegment(at: 2);
        echo(data: "\(String(describing: image))")

        //获取指定索引选项的标题titleForSegmentAtIndex
        let title = segment.titleForSegment(at: 3);
        echo(data: "\(String(describing: title))")

        segment.backgroundColor = UIColor.white
        segment.setBackgroundImage(UIImage.init(named: "dm5"), for: UIControlState.selected, barMetrics: UIBarMetrics.default)

        //设置普通状态下(未选中)状态下的文字颜色和字体
        segment.setTitleTextAttributes([NSFontAttributeName:FONT(s: 12), NSForegroundColorAttributeName:#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1)], for: UIControlState.normal)

        //设置选中状态下的文字颜色和字体
        segment.setTitleTextAttributes([NSFontAttributeName:FONT(s: 17), NSForegroundColorAttributeName:#colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)], for: UIControlState.selected)

        segment.addTarget(self, action: #selector(segmentClick(seg:)), for: UIControlEvents.valueChanged)
    }

    func segmentClick(seg:UISegmentedControl) -> Void {
        echo(data: "\(seg.selectedSegmentIndex)");
        switch seg.selectedSegmentIndex {
        case 0:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 1:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 2:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 3:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 4:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 5:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 6:
            echo(data: "\(seg.selectedSegmentIndex)")
        case 7:
            echo(data: "\(seg.selectedSegmentIndex)")
        default:
            echo(data: "最后")
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
